package com.zcyr.core.impl;

import com.zcyr.core.AbstractDatabaseEnvironment;
import com.zcyr.exception.CommandExecutorException;
import com.zcyr.pojo.SQLResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;


@Component
public class CommandExecutor {
    @Autowired
    SQLCommandMethods sqlMethods;

    public SQLResult executeCommand(String command, AbstractDatabaseEnvironment env) throws CommandExecutorException {
        try{
            switch (command.trim()) {
                case "\\d":
                    return sqlMethods.showTables(env);
                case "\\dn":
                    return sqlMethods.showSchema(env);
                case "\\du":
                    return sqlMethods.showUser(env);
                default:
                    throw new CommandExecutorException("执行失败, 未知命令 " + command);
            }
        } catch (SQLException e){
            e.printStackTrace();
            throw new CommandExecutorException("执行失败, 数据库异常, 命令 " + command);
        }
    }
}
